//
//  InformationViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/18.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "InformationViewController.h"
#import "InformationTableViewCell.h"
#import "MyInformationViewController.h"
#import "BuleModel.h"
#import "NoNetWorkingView.h"

@interface InformationViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *tableLabelArray;
    NSMutableDictionary *tableDic;
    NSInteger messageNum;
    UITableView *table;
    
    NoNetWorkingView *netWorkView;   //断网控件
    
}
@property (nonatomic,strong) UIButton *tryAgain;
@property (nonatomic,strong) UILabel *faile;
@property (nonatomic,strong)UILabel *faile2;

@property (nonatomic, strong) BuleModel *model;
@property (nonatomic,strong) YALSunnyRefreshControl *sunnyRefreshControl;
@end

@implementation InformationViewController
- (void)viewDidAppear:(BOOL)animated{

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUI];
    [self starAnimationHandle2];
}
- (void)viewWillDisappear:(BOOL)animated{
    //界面即将消失时移除监听者
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"noNetWork" object:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    //注册管擦者，接受网络状态观察者发送过来的关于网络的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showNoNetWorkTag:) name:@"noNetWork" object:nil];
}
/**
 *  绘制界面
 */
- (void)setUI{
    self.model = [[BuleModel alloc]init];
    if ([self.fromView isEqualToString:@"5"]) {
        [self getData];     //通讯录分类是固定的，不用进行网络请求，所有没有添加下拉刷新控件，这里手动进行一次获取数据
    }
    NSArray *titleArray =  @[@"个人设置",@"公文通知",@"会议管理",@"公文批阅",@"任务进程",@"通讯录",@"运营分析"];
    self.title = titleArray[[self.fromView intValue]];
    if ([self.fromView isEqualToString:@"3"] || [self.fromView isEqualToString:@"1"] || [self.fromView isEqualToString:@"4"]) {
        table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSWidth, KSHeight) style:UITableViewStylePlain];
    }else{
        table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSWidth, KSHeight) style:UITableViewStylePlain];
    }
    table.delegate = self;
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    table.dataSource = self;
    [self.view addSubview:table];
    if ([self.fromView isEqualToString:@"3"] || [self.fromView isEqualToString:@"1"]|| [self.fromView isEqualToString:@"4"]) {
        [self setupRefreshControl];
    }
    [self setFaileView];
}


/**
 *  加载界面失败时弹出的界面 ，默认先将他隐藏
 */
- (void)setFaileView{
    _tryAgain = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_tryAgain];
    [_tryAgain setImage:[UIImage imageNamed:@"失败再次提交l"] forState:UIControlStateNormal];
    _tryAgain.bounds = CGRectMake(0, 0, 150, 150);
    _tryAgain.center = self.view.center;
    _tryAgain.hidden = YES;
    [_tryAgain addTarget:self action:@selector(loadAgain) forControlEvents:UIControlEventTouchUpInside];
    
    _faile = [[UILabel alloc]init];
    [self.view addSubview:_faile];
    _faile.text = @"啊咧，似乎出了点问题...";
    _faile.hidden = YES;
    _faile.textAlignment = NSTextAlignmentCenter;
    _faile.textColor = [UIColor lightGrayColor];
    [_faile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tryAgain.mas_bottom).offset(5);
        make.width.mas_equalTo(KSWidth);
        make.centerX.equalTo(self.view);
    }];
    
    _faile2 = [[UILabel alloc]init];
    [self.view addSubview:_faile2];
    _faile2.text = @"点击重新加载";
    _faile2.hidden = YES;
    _faile2.textAlignment = NSTextAlignmentCenter;
    _faile2.textColor = [UIColor colorWithRed:0.788 green:0.074 blue:0.202 alpha:1.000];
    [_faile2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_faile.mas_bottom).offset(5);
        make.width.mas_equalTo(KSWidth);
        make.centerX.equalTo(self.view);
    }];
   
}
//根据网络状态观察者传来的值进行判断，如果为－1就显示控件
- (void)showNoNetWorkTag:(NSNotification *)sender{
    NSString *isConnection = [sender object];
    if ([isConnection isEqualToString:@"-1"]) {
        netWorkView = [[NoNetWorkingView alloc]initWithFrame:CGRectMake(0, 0, KSWidth, 35)];
        table.tableHeaderView = netWorkView;
        
    }else{
        //有网络，隐藏控件，并重新发送请求数据
        table.tableHeaderView = nil;
        [self sunnyControlDidStartAnimation];
        
    }
    
    
}

//显示加载失败按钮
- (void)getDataAgain{
    _tryAgain.hidden = NO;
    _faile.hidden = NO;
    _faile2.hidden = NO;
}

//重新加载
- (void)loadAgain{
    [self getData];
    [_tryAgain setHidden:YES];
    [_faile setHidden:YES];
    [_faile2 setHidden:YES];
}

# pragma mark - YALSunyRefreshControl methods

-(void)setupRefreshControl{
    self.sunnyRefreshControl = [YALSunnyRefreshControl new];
    [self.sunnyRefreshControl addTarget:self
                                 action:@selector(sunnyControlDidStartAnimation)
                       forControlEvents:UIControlEventValueChanged];
    [self.sunnyRefreshControl attachToScrollView:table];
}
//刷新执行事件
-(void)sunnyControlDidStartAnimation{
    
    [self getData];
    
}
//和主界面的开始动画区分开，同名会影响到主界面的刷新操作 PS:后来发现不会影响是其它原因影响，这里懒的改回来了
- (void)starAnimationHandle2{

    [self.sunnyRefreshControl beginRefreshing];

}

//停止动画
-(void)endAnimationHandle{
    
    [self.sunnyRefreshControl endRefreshing];
}

/**
 *  获取表格数据
 */
- (void)getData{
    [GMDCircleLoader setOnView:self.view withTitle:@"" animated:YES];
    __block InformationViewController *blockSelf = self;
    self.model.block = ^(BOOL success){
        if (success) {
            [blockSelf->table setHidden:NO];
            [blockSelf->table reloadData];
            [blockSelf->_faile setHidden:YES];
            [blockSelf->_faile2 setHidden:YES];
            [blockSelf->_tryAgain setHidden:YES];
        }else{
            [blockSelf getDataAgain];
            [blockSelf->table setHidden:YES];
        }
        [blockSelf endAnimationHandle];
        [GMDCircleLoader hideFromView:blockSelf.view animated:YES];
    };
    [_model getData:self.fromView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    tableLabelArray = self.model.tableDic[@"tableLabelArray"];
    return tableLabelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60*FitSize;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellID = @"CellID";
    InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        cell = [[InformationTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setWithDic:self.model.tableDic index:indexPath.row];
    return cell;
}
/**
 *  跳转界面
 *
 *  @param tableView 操作表格
 *  @param indexPath 行数
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyInformationViewController *myInformation = [[MyInformationViewController alloc]init];
    myInformation.fromView = self.fromView;
    if ([self.fromView isEqualToString:@"1"] || [self.fromView isEqualToString:@"3"] || [self.fromView isEqualToString:@"4"]) {
        //tableID是咨询分类：eg   －－－ 所有资讯  通知资讯  企业新闻 ....
        NSArray *IDArray = self.model.tableDic[@"tableId"];
        NSLog(@"任务进度分类id%@",IDArray);
        myInformation.listType = IDArray[indexPath.row];
    }else{
        myInformation.listType = [NSString stringWithFormat:@"%ld",indexPath.row];
    }
    myInformation.title = self.model.tableDic[@"tableLabelArray"][indexPath.row];
    [self.navigationController pushViewController:myInformation animated:YES];
    
}


@end
